from django.db import models


# Create your models here.
class Enterprise(models.Model):
    name = models.CharField(max_length=30, unique=True)
    intro = models.TextField(max_length=1024, null=True)
    industry = models.CharField(max_length=30, null=True)
    contact_way = models.CharField(max_length=63, null=True)
    image_key = models.CharField(max_length=60, null=True)
    follow_count = models.IntegerField(default=0, null=False)
    creator_name = models.CharField(max_length=31, null=False, default="default_creator_01")


    def parse_dict(self):
        return {
            "name": self.name,
            "intro": self.intro,
            "class": self.industry,
            "contactWay": self.contact_way,
            "imageKey": self.image_key,
            "followCount": self.follow_count,
            "creator_name": self.creator_name
        }


class Enterprise_Usr(models.Model):  # 企业_用户关系表
    id = models.BigAutoField(null=False, primary_key=True)
    usr = models.ForeignKey('account_sys.User', on_delete=models.SET_DEFAULT, default='用户已注销')
    enterprise = models.ForeignKey('Enterprise', on_delete=models.CASCADE, default='企业不存在')
    is_admin = models.BooleanField(default=False)  # 是否为企业管理员
    is_agree = models.BooleanField(default=False)  # 是否通过认证


class Employee_info(models.Model):
    e_u = models.ForeignKey(to=Enterprise_Usr, to_field='id', on_delete=models.CASCADE)
    name = models.CharField(max_length=30)
    position = models.CharField(max_length=30)
    contact_way = models.CharField(max_length=100, default='')

    def parse_dict(self):
        return {
            "name": self.name,
            "position": self.position,
            "contact_way": self.contact_way
        }

class Job(models.Model):
    user = models.ForeignKey(Enterprise_Usr, verbose_name="企业-员工", on_delete=models.CASCADE, default=1)
    enterprise = models.ForeignKey(Enterprise, verbose_name="企业-公司", on_delete=models.CASCADE)
    name = models.CharField(max_length=30, default='')
    job_count = models.IntegerField(verbose_name="招聘人数", default=0)
    salary = models.FloatField(verbose_name="薪水", default=0.0)
    requirement = models.CharField(max_length=300, default='')
    address = models.CharField(max_length=255, default='空')
    education = models.CharField(max_length=50, default='空')

    def parse_dict(self):
        return {
            "id": self.id,
            "enterprise_id": self.enterprise.id,
            "name": self.name,
            "job_count": self.job_count,
            "salary": self.salary,
            "address": self.address,
            "education": self.education,
            "requirement": self.requirement
        }

class Resume(models.Model):
    job = models.ForeignKey(Job, verbose_name="招聘的岗位", on_delete=models.CASCADE)
    user = models.ForeignKey('account_sys.User', on_delete=models.CASCADE, default='用户')
    is_employ = models.BooleanField(default=False, verbose_name="是否已录用")
    resume_file = models.CharField(max_length=1024, default='', verbose_name="简历PDF的URL")
    created_at = models.DateTimeField(auto_now_add=True)

    def parse_dict(self):
        return {
            "id": self.id,
            "job": self.job.parse_dict(),
            "user": self.user.info_to_dict(),
            "is_employ": self.is_employ,
            "resume_file": self.resume_file
        }




